package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.SpChargeItemsMapper;
import com.one.group.admin.mapper.SpChargeStandardsMapper;
import com.one.group.model.dto.SpChargeItemAddDto;
import com.one.group.model.dto.SpChargeItemsQueryDto;
import com.one.group.model.dto.SpChargeItemsUpdateDto;
import com.one.group.model.entity.SpChargeItems;
import com.one.group.model.entity.SpChargeStandards;
import com.one.group.model.vo.SpChargeItemsVo;
import com.one.group.service.SpChargeItemsService;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpChargeItemsServiceImpl extends ServiceImpl<SpChargeItemsMapper, SpChargeItems> implements SpChargeItemsService {
    @Autowired
    private SpChargeStandardsMapper spChargeStandardsMapper;
    @Override
    public List<SpChargeItems> findByChargeTypeId(Long chargeTypeId) {
        // 1. 创建查询条件
        QueryWrapper queryWrapper = QueryWrapper.create().from(SpChargeItems.class);
        queryWrapper.eq(SpChargeItems::getChargetypeId, chargeTypeId);
        // 2. 执行查询
        return getMapper().selectListByQuery(queryWrapper);
    }

    @Override
    public List<SpChargeItemsVo> findListByQuery(SpChargeItemsQueryDto spChargeItemsQueryDto) {
        //获取租户id
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return getMapper().mySelectListByQuery(spChargeItemsQueryDto.getName(), tenant_id);
    }

    @Override
    public boolean myAdd(SpChargeItemAddDto spChargeItemsAddDto) {
        try{
            //获取租户id
            Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
            SpChargeItems spChargeItems = new SpChargeItems();
            spChargeItems.setName(spChargeItemsAddDto.getName());
            spChargeItems.setChargetypeId(spChargeItemsAddDto.getChargetypeId());
            spChargeItems.setRemarks(spChargeItemsAddDto.getRemarks());
            spChargeItems.setTenantId(tenant_id);
            return getMapper().insert(spChargeItems, true) == 1;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean myUpdate(SpChargeItemsUpdateDto spChargeItemsUpdateDto) {
        try{
            SpChargeItems spChargeItems = new SpChargeItems();
            spChargeItems.setId(spChargeItemsUpdateDto.getId());
            spChargeItems.setName(spChargeItemsUpdateDto.getName());
            spChargeItems.setChargetypeId(spChargeItemsUpdateDto.getChargetypeId());
            spChargeItems.setRemarks(spChargeItemsUpdateDto.getRemarks());
            return getMapper().update(spChargeItems, true) == 1;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean myRemoveById(Long id) {
        //判断是否存在相关的数据
        QueryWrapper queryWrapper = QueryWrapper.create().from(SpChargeStandards.class);
        queryWrapper.eq(SpChargeStandards::getChargeitemId, id);
        if (spChargeStandardsMapper.selectCountByQuery(queryWrapper) > 0){
            return false;
        }
        return removeById(id);
    }
}
